<?php

namespace Admin\Controller\Analysis;

use Helper\AnalysisApi;

/**
 * Class AnalysisController
 * @package Admin\Controller
 */
class AnalysisOfferController extends AnalysisController
{
	protected $_tpl_index = 'Analysis/Index';
	protected $_tpl_search = 'Analysis/Offer';

    function _before_searchdata(array $post)
    {
        //禁止跨索引查询数据
        if(date("Ym", strtotime($post["end_day"])) - date("Ym", strtotime($post["start_day"])) > 1)
        {
            $this->error("不能跨月查询数据");
            return false;
        }

        if(strtotime($post["end_day"]) - strtotime($post["start_day"]) > 86400 * 7)
        {
            if(empty($post["offer_id"]) && empty($post["country_id"]))

                $this->error("不能查询超过一周的数据");
            return false;
        }
    }

	function searchdata(array $search)
	{
        $list = [];

		//非拦截状态
		if (!$search['rob'])
        {
            $search['index'] = "eventlogs";
            $search['source'] = 'show,show_user,click,click_user,installed,installed_user';
            $search['aggs'] = "date,channel,vcode";
            $search['mark'] = CONTROLLER_NAME . "-tian";
            try {
                $eventlogs = AnalysisApi::getResponse($search);
            } catch (\Exception $e) {
                $this->error(APP_DEBUG ? $e->getMessage() : "系统错误，请通知管理员");
            }

            if (!empty($eventlogs))
            {
                $dates = range(strtotime($search['dateStart']), strtotime($search['dateEnd']), 86400);
                foreach ($eventlogs as $val)
                {
                    if (!in_array(strtotime($val['date']), $dates))
                    {
                        continue;
                    }
                    $list[$val['date']][$val['channel']][$val['vcode']]['show'] += intval($val['show']);
                    $list[$val['date']][$val['channel']][$val['vcode']]['show_user'] += intval($val['show_user']);
                    $list[$val['date']][$val['channel']][$val['vcode']]['click'] += intval($val['click']);
                    $list[$val['date']][$val['channel']][$val['vcode']]['click_user'] += intval($val['click_user']);
                    $list[$val['date']][$val['channel']][$val['vcode']]['installed'] += intval($val['installed']);
                    $list[$val['date']][$val['channel']][$val['vcode']]['installed_user'] += intval($val['installed_user']);
                }
            }
        }

        //收益
        $search['index'] = "postback";
        $search['source'] = 'conversion,conversion_user,income';
        $search['aggs'] = "date,channel,vcode";
        $search['mark'] = CONTROLLER_NAME . "-tian";
        try {
            $postback = AnalysisApi::getResponse($search);
        } catch (\Exception $e) {
            $this->error(APP_DEBUG ? $e->getMessage() : "系统错误，请通知管理员");
        }

        if (!empty($postback))
        {
            $dates = range(strtotime($search['dateStart']), strtotime($search['dateEnd']), 86400);
            foreach ($postback as $k => $v)
            {
                if (!in_array(strtotime($v['date']), $dates))
                {
                    continue;
                }
                $list[$v['date']][$v['channel']][$v['vcode']]['conversion'] += intval($v['conversion']);
                $list[$v['date']][$v['channel']][$v['vcode']]['conversion_user'] += intval($v['conversion_user']);
                $list[$v['date']][$v['channel']][$v['vcode']]['income'] += intval($v['income']);
            }
        }

		return $list;
	}
}
